import 'package:mobx/mobx.dart';
import 'package:thal/backstage/blueprint/follow_print.dart';

part 'follow_room.g.dart';

/// 关注状态管理
class FollowRoom = _FollowRoom with _$FollowRoom;

abstract class _FollowRoom with Store {
  /// 所有关注关系列表
  @observable
  ObservableList<FollowPrint> follows = ObservableList<FollowPrint>();

  /// 初始化默认关注数据
  @action
  void initializeDefaultFollows() {
    follows = ObservableList<FollowPrint>.of([
      // admin (id: '1') 关注其他用户
      FollowPrint(
        id: '1',
        followerId: '1', // admin
        followingId: '2', // Caleb
        createdAt: DateTime(2024, 1, 15),
      ),
      FollowPrint(
        id: '2',
        followerId: '1', // admin
        followingId: '3', // Evan
        createdAt: DateTime(2024, 1, 20),
      ),
      FollowPrint(
        id: '3',
        followerId: '1', // admin
        followingId: '4', // Louis
        createdAt: DateTime(2024, 2, 5),
      ),
      FollowPrint(
        id: '4',
        followerId: '1', // admin
        followingId: '6', // Alden
        createdAt: DateTime(2024, 2, 10),
      ),
      FollowPrint(
        id: '5',
        followerId: '1', // admin
        followingId: '9', // Zoe
        createdAt: DateTime(2024, 2, 15),
      ),

      // 其他用户关注 admin
      FollowPrint(
        id: '6',
        followerId: '2', // Caleb
        followingId: '1', // admin
        createdAt: DateTime(2024, 1, 16),
      ),
      FollowPrint(
        id: '7',
        followerId: '3', // Evan
        followingId: '1', // admin
        createdAt: DateTime(2024, 1, 21),
      ),
      FollowPrint(
        id: '8',
        followerId: '4', // Louis
        followingId: '1', // admin
        createdAt: DateTime(2024, 2, 6),
      ),
      FollowPrint(
        id: '9',
        followerId: '5', // Caleb
        followingId: '1', // admin
        createdAt: DateTime(2024, 2, 8),
      ),
      FollowPrint(
        id: '10',
        followerId: '6', // Alden
        followingId: '1', // admin
        createdAt: DateTime(2024, 2, 11),
      ),

      // 其他用户之间的关注关系
      FollowPrint(
        id: '11',
        followerId: '2', // Caleb
        followingId: '3', // Evan
        createdAt: DateTime(2024, 1, 25),
      ),
      FollowPrint(
        id: '12',
        followerId: '3', // Evan
        followingId: '4', // Louis
        createdAt: DateTime(2024, 2, 1),
      ),
      FollowPrint(
        id: '13',
        followerId: '4', // Louis
        followingId: '5', // Caleb
        createdAt: DateTime(2024, 2, 3),
      ),
      FollowPrint(
        id: '14',
        followerId: '6', // Alden
        followingId: '7', // Milo
        createdAt: DateTime(2024, 2, 12),
      ),
      FollowPrint(
        id: '15',
        followerId: '7', // Milo
        followingId: '8', // Julian
        createdAt: DateTime(2024, 2, 14),
      ),
      FollowPrint(
        id: '16',
        followerId: '8', // Julian
        followingId: '9', // Zoe
        createdAt: DateTime(2024, 2, 16),
      ),
      FollowPrint(
        id: '17',
        followerId: '9', // Zoe
        followingId: '10', // Liam
        createdAt: DateTime(2024, 2, 18),
      ),
      FollowPrint(
        id: '18',
        followerId: '10', // Liam
        followingId: '2', // Caleb
        createdAt: DateTime(2024, 2, 20),
      ),
    ]);
  }

  /// 关注用户
  @action
  bool performFollow(String followerId, String followingId) {
    try {
      // 检查是否已经关注
      if (isFollowing(followerId, followingId)) {
        return false;
      }

      // 创建新的关注关系
      final newFollow = FollowPrint(
        id: (follows.length + 1).toString(),
        followerId: followerId,
        followingId: followingId,
        createdAt: DateTime.now(),
      );

      follows.add(newFollow);
      return true;
    } catch (e) {
      return false;
    }
  }

  /// 取消关注
  @action
  bool performUnfollow(String followerId, String followingId) {
    try {
      final index = follows.indexWhere(
        (follow) =>
            follow.followerId == followerId &&
            follow.followingId == followingId,
      );

      if (index != -1) {
        follows.removeAt(index);
        return true;
      }
      return false;
    } catch (e) {
      return false;
    }
  }

  /// 检查是否已关注
  bool isFollowing(String followerId, String followingId) {
    return follows.any(
      (follow) =>
          follow.followerId == followerId && follow.followingId == followingId,
    );
  }

  /// 获取用户的关注列表
  List<String> fetchFollowingList(String userId) {
    return follows
        .where((follow) => follow.followerId == userId)
        .map((follow) => follow.followingId)
        .toList();
  }

  /// 获取用户的粉丝列表
  List<String> fetchFollowersList(String userId) {
    return follows
        .where((follow) => follow.followingId == userId)
        .map((follow) => follow.followerId)
        .toList();
  }

  /// 获取关注数量
  int fetchFollowingCount(String userId) {
    return follows.where((follow) => follow.followerId == userId).length;
  }

  /// 获取粉丝数量
  int fetchFollowersCount(String userId) {
    return follows.where((follow) => follow.followingId == userId).length;
  }

  /// 获取所有关注关系
  List<FollowPrint> fetchAllFollows() {
    return List.from(follows);
  }

  /// 根据ID获取关注关系
  FollowPrint? fetchFollowById(String followId) {
    try {
      return follows.firstWhere((follow) => follow.id == followId);
    } catch (e) {
      return null;
    }
  }
}
